<?
class langs_page_controller extends page {
   
  protected $Message = null; 
  protected $Phrases = array();
  
  //list of phrase constants found in code files
  protected $Constants = array(); 
  
  public function prepare() {
  }
  
  public function index($Params) {  
    //
  }
  
  //convert language text file to pso file (PHP Serialised Object)
  private function txt_to_pso($File_Name,$Save=1) {
    $Txt_File = "langs/".$File_Name;
    $Pso_File = str_replace(".txt",".pso",$Txt_File);
    
    //read text lines into array
    $Lines = file($Txt_File);
    foreach ($Lines as $Key=>$Value) 
      $Lines[$Key] = trim($Value);

    //split each line into phrase constant and phrase translation
    $Phrases = array();
    foreach ($Lines as $Line) {
      $Pos = strpos($Line,"=");
      if ($Pos===false || $Pos==0)
        continue;      
        
      //split phrase constant and phrase translation in each line  
      $Left  = trim(substr($Line,0,$Pos));
      $Right = trim(substr($Line,$Pos+1));
      if (strlen($Left)==0 || strlen($Right)==0)
        continue;
        
      $Phrases[$Left] = $Right;
    }//foreach to split
    
    if ($Save==1)
      file_put_contents($Pso_File,serialize($Phrases));
    return $Phrases;
  }
  
  private function scan_for_phrases($File_Path) { 
    $Lines   = file($File_Path);
    $Marking = "__(\"";    
    
    foreach ($Lines as $Line) {
      $Pos = strpos($Line,$Marking);
      if ($Pos===false)
        continue;
        
      //get the spanning of phrase constant  
      $Start = $Pos+strlen($Marking);
      $End   = $Start;
      $Len   = strlen($Line);
      while ($End<$Len && $Line[$End]!="\"")
        $End++;
        
      //extract the phrase constant
      $Const = substr($Line,$Start,$End-$Start); 
      if (!in_array($Const,$this->Constants)) 
        array_push($this->Constants,$Const);
    }//foreach
  }
  
  private function scan_dir($Path) {
    $Items = scandir($Path);
    foreach ($Items as $Item) {
      $Subpath = $Path."/".$Item;
      if ($Item=="." || $Item=="..")
        continue;
        
      //check if is php file  
      if (is_file($Subpath)) {
        $Ext = utils::get_file_ext($Subpath);
        if ($Ext!="php")
          continue;
      }
        
      //find phrases in file, or go to subdir  
      if (is_file($Subpath))
        $this->scan_for_phrases($Subpath);
      else
      if (is_dir($Subpath))
        $this->scan_dir($Subpath);
    }//foreach
  }

  private function scan_pages($Pages) {
    foreach ($Pages as $Page) {
      $Title = strtoupper($Page["@attributes"]["title"]);
      $Title = str_replace(chr(32),"",$Title);
      if (!in_array($Title,$this->Constants))
        array_push($this->Constants,$Title);      
      
      //sub nodes
      if (val($Page,"page")!=null)
        $this->scan_pages($Page["page"]);
    }
  }
  
  private function scan_module_xml($File) {
    $File_Path = "modules/".$File;
    $Configs = xml::arr_from_file($File_Path);
    
    //add module title
    $Title = strtoupper($Configs["title"]);
    $Title = str_replace(chr(32),"",$Title);
    if (!in_array($Title,$this->Constants))
      array_push($this->Constants,$Title);
      
    //scan module page nodes    
    $this->scan_pages($Configs["menu"]["page"]);
  }
  
  private function clear_lang_psos() {
    $Files = utils::get_files_in("langs","pso");
    foreach ($Files as $File) {
      $File_Path = "langs/".$File;
      if (is_file($File_Path))
        unlink($File_Path);
    }
  }
  
  private function scan_page_xmls() {
    $Files = utils::get_files_in("modules");    
    foreach ($Files as $File) {
      if (!is_dir("modules/".$File))
        continue;
      if ($File=="." || $File=="..")
        continue;
     
      $Path = "modules/".$File."/pages/views";
      $Xmls = utils::get_files_in($Path,"xml");
      foreach ($Xmls as $Xml_File) {
        $Xml_Path = $Path."/".$Xml_File;
        $Page_Xml = xml::arr_from_file($Xml_Path);
        $Title    = strtoupper($Page_Xml["title"]);
        $Title    = str_replace(chr(32),"",$Title);
        
        if (!in_array($Title,$this->Constants))
          array_push($this->Constants,$Title);
      }
    }//foreach file
  }
  
  public function scan($Params) {
    $this->scan_dir(".");
    $this->scan_page_xmls();
    
    //additional phrase constants in config.xml
    $Website = strtoupper(system::$Configs["website"]);
    $Website = str_replace(chr(32),"",$Website);
    if (!in_array($Website,$this->Constants))
      array_push($this->Constants,$Website);

    //additional phrase constants in module config files
    $Files = utils::get_files_in("modules","xml"); 
    foreach ($Files as $File)
      if (is_file("modules/".$File))
        $this->scan_module_xml($File);    
    
    sort($this->Constants);
    if (val($Params,"inserting")=="yes")
      $this->insert_to_txt_files();
  }
  
  //insert the found phrases into language .txt files
  //not inserting if already exist
  private function insert_to_txt_files() {
    $Files  = utils::get_files_in("langs","txt");
    $Counts = array();    
    foreach ($Files as $File) {
      $Phrases = $this->txt_to_pso($File,$Save=0);
      
      //check all phrase constants found
      $Counts[$File] = 0;
      foreach ($this->Constants as $Const)
        if (!array_key_exists($Const,$Phrases)) {
          $Phrases[$Const] = "";
          $Counts[$File]++;
        }
      ksort($Phrases);
                
      //build text content
      $Text = ""; 
      foreach ($Phrases as $Key=>$Value) { 
        
        //keep only phrase constants being used,
        //skip those no longer used
        if (!in_array($Key,$this->Constants))
          continue;
      
        //for those phrases not yet translated
        if (strlen($Value)==0)
          $Value = "???";
          
        //make normal key,value pair  
        $Text .= $Key." = ".$Value."\r\n";
      }
        
      //save to file
      $Text = trim($Text);
      file_put_contents("langs/".$File,$Text);
    }//for
    
    $this->Message = phrase::__("LANGUAGE_TEXT_FILES_UPDATED_SUCCESSFULLY");
    $this->Message .= ":<br/>";
    foreach ($Counts as $File=>$Count)
      $this->Message .= $File.chr(32)."(".$Count.chr(32).
      phrase::__("NEW",0)."); ";
  }
  
  public function build($Params) {
    $Files = utils::get_files_in("langs","txt");
    $Count = 0;
    foreach ($Files as $File) { 
      $this->txt_to_pso($File);
      $Count++;
    }
      
    $this->Txt_Files = $Files;
    $this->Pso_Files = utils::get_files_in("langs","pso");
    $this->Message = phrase::__("SUCCESSFULLY_BUILT").chr(32).
    $Count.chr(32).phrase::__("LANGUAGE_FILES",0);
  }
  
  public function show($Params) {
    $this->Langs   = helper::get_lang_list();  
    
    //check param in url
    $Lang = val($Params,"lang");
    if ($Lang==null)
      return;
      
    //check file existance
    $Pso_File = "langs/lang".$Lang.".pso";
    if (!file_exists($Pso_File))
      return;      
    
    $this->Phrases = utils::var_from_pso($Pso_File);
  }
  
  public function clear($Params) {
    if (val($Params,"confirmed")=="yes") {
      $this->clear_lang_psos();
      $this->Message = 
      phrase::__("ALL_PSO_LANGUAGE_FILES_HAVE_BEEN_REMOVED");
    }
    else {
      $this->Message = 
      phrase::__("CLICK_THE_LINK_BELOW_TO_DO_THE_ACTUAL_CLEARING");
    }
  }
  
  public function finish() {
  }
}